package com.chenju.dynamicagent.aop;

import com.chenju.dynamicagent.annotation.EnableDynamicAgent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DynamicAgentAspect {

    @Pointcut("execution(* com.chenju.dynamicagent..*(..))")
    // @Pointcut("@annotation(com.chenju.dynamicagent.annotation.EnableDynamicAgent)")
    public void pointcut(){}

    @Around("pointcut()")
    public Object Around(ProceedingJoinPoint pjp){
        Object result = null;
        // 执行前置方法
        AroundMethod.before(pjp);

        // 执行当前方法
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        // 执行后置方法
        AroundMethod.after(pjp);

        return result;
    }
}
